package com.huang.exp03.controller;

import com.huang.exp03.model.Units;
import com.huang.exp03.services.InitServices;
import com.huang.exp03.services.SearchServices;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataController {
    @RequestMapping("/")
    public String home(Model model){
        int total=0;
        int plus=0;
        for(Units units: InitServices.unitsList){
            total+=units.getLatestTotalCases();
            plus+=units.getDiffFromPrevDay();
        }
        model.addAttribute("total",total);
        model.addAttribute("plus",plus);
        model.addAttribute("unitsList",InitServices.unitsList);
        return "index";
    }
    @RequestMapping("/search")
    public String search(Model model,String key){
        int total=0;
        int plus=0;
        SearchServices searchServices=new SearchServices();
        for(Units units: searchServices.search(key)){
            total+=units.getLatestTotalCases();
            plus+=units.getDiffFromPrevDay();
        }
        model.addAttribute("total",total);
        model.addAttribute("plus",plus);
        model.addAttribute("keyList",searchServices.search(key));
        return "search";
    }
}
